package visiopuce.service.document;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Display;

import visiopuce.ApplicationContexte;
import visiopuce.objets.Analyse;
import visiopuce.objets.Champ;
import visiopuce.objets.Personne;
import visiopuce.objets.Phenotype;
import visiopuce.objets.Prelevement;
import visiopuce.service.AnalyseServiceImpl;
import visiopuce.service.PersonneServiceImpl;
import visiopuce.service.PrelevementServiceImpl;
import fr.opensagres.xdocreport.core.XDocReportException;
import fr.opensagres.xdocreport.document.IXDocReport;
import fr.opensagres.xdocreport.document.registry.XDocReportRegistry;
import fr.opensagres.xdocreport.template.IContext;
import fr.opensagres.xdocreport.template.TemplateEngineKind;

public class FicheCultureDoc {

	public String createDocument(Prelevement prelevement, String type, String secteur) {
		String nomFichierout = "";
		try {
			boolean creer = true;
			InputStream in = null;
			in = new FileInputStream(new File(ApplicationContexte.REPERTOIRE_MODEL + "FicheCulture.docx"));

			IXDocReport report = XDocReportRegistry.getRegistry().loadReport(in, TemplateEngineKind.Velocity);
			Personne personne = PersonneServiceImpl.getInstance().getPersonneById(prelevement.getPersonne().getIdPersonne());
			SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");

			String indication = "";
			if (prelevement.getIndication() == null || prelevement.getIndication().getLibelle().equals("non renseigné")) {
				indication = "L'INDICATION N'A PAS ETE ENTREE";
			}
			if (prelevement.getIndication() != null && !prelevement.getIndication().getLibelle().contains("utre")) {
				indication = prelevement.getIndication().getLibelle() + " - ";
			}
			Champ ctSigneAppel = PrelevementServiceImpl.getInstance().getChampByTypeAndIdObjet("tSigneAppel", "Prel-" + prelevement.getIdPrelevement());
			if (prelevement.getIndication() != null && prelevement.getIndication().getLibelle().contains("ignes d'appel")) {
				if (ctSigneAppel != null && ctSigneAppel.getValeur() != null && !ctSigneAppel.getValeur().equals("null")) {
					indication = "Signes d'appel échographique : " + ctSigneAppel.getValeur() + " - ";
				}
			} else {
				if (ctSigneAppel != null && ctSigneAppel.getValeur() != null && !ctSigneAppel.getValeur().equals("null")) {
					indication += "signes d'appel échographique : " + ctSigneAppel.getValeur() + " - ";
				}
			}
			if (personne.getPhenotype() != null && !personne.getPhenotype().isEmpty()) {
				indication += personne.getPhenotype() + " - ";
			}
			ArrayList<Phenotype> pheno = PersonneServiceImpl.getInstance().getPhenotypesByIdPersonne(personne.getIdPersonne());
			if (pheno != null) {
				for (Phenotype phenotype : pheno) {
					if (!indication.toLowerCase().contains(phenotype.getLibelle().toLowerCase())) {
						indication += phenotype.getLibelle() + " - ";
					}
				}
			}
			if (prelevement.getInfoIndication() != null && !prelevement.getInfoIndication().isEmpty()) {
				indication += prelevement.getInfoIndication() + " - ";
			}
			if (secteur.contains("Prénatal")) {
				Date ddg = null;
				Champ champdateDG = PrelevementServiceImpl.getInstance().getChampByTypeAndIdObjet("dateDG", "Prel-" + prelevement.getIdPrelevement());
				if (champdateDG != null && champdateDG.getValeur() != null && !champdateDG.getValeur().equals("null")) {
					SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
					try {
						ddg = formater.parse(champdateDG.getValeur());
						indication += "\r\nDate de début de grossesse : " + formatter.format(ddg);
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}

				Champ cTerme = PrelevementServiceImpl.getInstance().getChampByTypeAndIdObjet("tTerme", "Prel-" + prelevement.getIdPrelevement());
				if (cTerme != null && cTerme.getValeur() != null && !cTerme.getValeur().equals("null")) {
					indication += "\r\nTerme :" + cTerme.getValeur();
				}

			}
			IContext context = report.createContext();
			String analysesDemandees = "";
			if (context.get("prescripteur") == null) {
				context.put("prescripteur", prelevement.getPrescripteur());
			}
			List<Analyse> analyses = AnalyseServiceImpl.getInstance().findAllByidPrelevement(prelevement.getIdPrelevement());
			for (Analyse analyse : analyses) {
				analysesDemandees += analyse.getTypeAnalyse().getLibelle();
				if (analyse.isUrgent()) {
					analysesDemandees += " URGENT";
					if (analyse.getRaisonUrgent() != null) {
						analysesDemandees += "(" + analyse.getRaisonUrgent() + ")";
					}

				}
				analysesDemandees += "\r\n";

				// caryo
				if (analyse.getTypeAnalyse().getIdTypeAnalyse() == 1) {

					Champ ctResolutionCaryo = PrelevementServiceImpl.getInstance().getChampByTypeAndIdObjet("tResolutionCaryo", "Ana-" + analyse.getIdAnalyse());
					if (ctResolutionCaryo != null && ctResolutionCaryo.getValeur() != null && !ctResolutionCaryo.getValeur().equals("null")) {
						context.put("resolutionBio", ctResolutionCaryo.getValeur());
					} else {
						context.put("resolutionBio", "900 bandes - 850 bandes - 700 bandes - 550 bandes - 400-550 bandes - 400 bandes - 300 bandes");
					}

					Champ cbtnMarquage = PrelevementServiceImpl.getInstance().getChampByTypeAndIdObjet("btnMarquage", "Ana-" + analyse.getIdAnalyse());
					if (cbtnMarquage != null && cbtnMarquage.getValeur() != null && !cbtnMarquage.getValeur().equals("null")) {
						context.put("marquageBio", cbtnMarquage.getValeur());

					} else {
						context.put("marquageBio", "GTG - RHG - Q - bandes C - NOR");
					}
					String mitosesCla = "";
					Champ ctMitosesClassees = PrelevementServiceImpl.getInstance().getChampByTypeAndIdObjet("tMitosesClassees", "Ana-" + analyse.getIdAnalyse());
					if (ctMitosesClassees != null && ctMitosesClassees.getValeur() != null && !ctMitosesClassees.getValeur().equals("null")) {
						mitosesCla = ctMitosesClassees.getValeur();

					}
					if (secteur != null && secteur.equals(ApplicationContexte.SECTEUR_PRENATAL)) {
						Champ ctColonies = PrelevementServiceImpl.getInstance().getChampByTypeAndIdObjet("tColonies", "Ana-" + analyse.getIdAnalyse());
						if (ctColonies != null && ctColonies.getValeur() != null && !ctColonies.getValeur().equals("null")) {
							mitosesCla += "\r\nNombre de colonies : " + ctColonies.getValeur();
						}
						Champ ctChambresCultures = PrelevementServiceImpl.getInstance().getChampByTypeAndIdObjet("tChambresCultures", "Ana-" + analyse.getIdAnalyse());
						if (ctChambresCultures != null && ctChambresCultures.getValeur() != null && !ctChambresCultures.getValeur().equals("null")) {
							mitosesCla += "\r\nNombre de chambres de culture : " + ctChambresCultures.getValeur();
						}
					}
					context.put("mitosesCla", mitosesCla);

					Champ ctMitosesExaminees = PrelevementServiceImpl.getInstance().getChampByTypeAndIdObjet("tMitosesExaminees", "Ana-" + analyse.getIdAnalyse());
					if (ctMitosesExaminees != null && ctMitosesExaminees.getValeur() != null && !ctMitosesExaminees.getValeur().equals("null")) {
						context.put("mitosesEx", ctMitosesExaminees.getValeur());
					} else {
						context.put("mitosesEx", "");
					}

					Champ ccIndexMito = PrelevementServiceImpl.getInstance().getChampByTypeAndIdObjet("cIndexMito", "Ana-" + analyse.getIdAnalyse());
					if (ccIndexMito != null && ccIndexMito.getValeur() != null && !ccIndexMito.getValeur().equals("null")) {
						context.put("indexMitotique", ccIndexMito.getValeur());
					} else {
						context.put("indexMitotique", "+  +/-  --");
					}
					Champ cclChr = PrelevementServiceImpl.getInstance().getChampByTypeAndIdObjet("clChr", "Ana-" + analyse.getIdAnalyse());
					if (cclChr != null && cclChr.getValeur() != null && !cclChr.getValeur().equals("null")) {
						context.put("longueurChr", cclChr.getValeur());
					} else {
						context.put("longueurChr", "+  +/-  --");
					}
					Champ ccCytoplasme = PrelevementServiceImpl.getInstance().getChampByTypeAndIdObjet("cCytoplasme", "Ana-" + analyse.getIdAnalyse());
					if (ccCytoplasme != null && ccCytoplasme.getValeur() != null && !ccCytoplasme.getValeur().equals("null")) {
						context.put("cytoplasme", ccCytoplasme.getValeur());
					} else {
						context.put("cytoplasme", "+  +/-  --");
					}
					Champ ccMitosesEclat = PrelevementServiceImpl.getInstance().getChampByTypeAndIdObjet("cMitosesEclat", "Ana-" + analyse.getIdAnalyse());
					if (ccMitosesEclat != null && ccMitosesEclat.getValeur() != null && !ccMitosesEclat.getValeur().equals("null")) {
						context.put("mitoseEclatees", ccMitosesEclat.getValeur());
					} else {
						context.put("mitoseEclatees", "+  +/-  --");
					}
					Champ ccBandeR = PrelevementServiceImpl.getInstance().getChampByTypeAndIdObjet("cBandeR", "Ana-" + analyse.getIdAnalyse());
					if (ccBandeR != null && ccBandeR.getValeur() != null && !ccBandeR.getValeur().equals("null")) {
						context.put("bandesR", ccBandeR.getValeur());
					} else {
						context.put("bandesR", "+  +/-  --");
					}
					Champ ccBandeG = PrelevementServiceImpl.getInstance().getChampByTypeAndIdObjet("cBandeG", "Ana-" + analyse.getIdAnalyse());
					if (ccBandeG != null && ccBandeG.getValeur() != null && !ccBandeG.getValeur().equals("null")) {
						context.put("bandesG", ccBandeG.getValeur());
					} else {
						context.put("bandesG", "+  +/-  --");
					}
					Champ ctResolutionCaryoTech = PrelevementServiceImpl.getInstance().getChampByTypeAndIdObjet("tResolutionCaryoTech", "Ana-" + analyse.getIdAnalyse());
					if (ctResolutionCaryoTech != null && ctResolutionCaryoTech.getValeur() != null && !ctResolutionCaryoTech.getValeur().equals("null")) {
						context.put("resolutionTech", ctResolutionCaryoTech.getValeur());
					} else {
						context.put("resolutionTech", "900 bandes - 850 bandes - 700 bandes - 550 bandes - 400-550 bandes - 400 bandes - 300 bandes");
					}
					Champ ctFormuleTech = PrelevementServiceImpl.getInstance().getChampByTypeAndIdObjet("tFormuleTech", "Ana-" + analyse.getIdAnalyse());
					if (ctFormuleTech != null && ctFormuleTech.getValeur() != null && !ctFormuleTech.getValeur().equals("null")) {
						context.put("formuleTech", ctFormuleTech.getValeur());
					} else {
						context.put("formuleTech", "");
					}
					Champ ctRemarqueTech = PrelevementServiceImpl.getInstance().getChampByTypeAndIdObjet("tRemarqueTech", "Ana-" + analyse.getIdAnalyse());
					if (ctRemarqueTech != null && ctRemarqueTech.getValeur() != null && !ctRemarqueTech.getValeur().equals("null")) {
						context.put("remarqueTech", ctRemarqueTech.getValeur());
					} else {
						context.put("remarqueTech", "");
					}
					context.put("formuleBio", "");

				}

			}
			context.put("analysesDemandees", analysesDemandees);
			context.put("indication", indication + ".");
			context.put("personne", personne);
			context.put("dateNaissance", formatter.format(personne.getDateNaissance()));
			if (prelevement.getDatePrelevement() != null) {
				context.put("datePrelevement", formatter.format(prelevement.getDatePrelevement()));
			} else {
				context.put("datePrelevement", "");
			}
			context.put("prelevement", prelevement);

			Date dmc = null;
			String tubes = "";
			if (type.equals("F")) {
				Champ ccomboVolFibro = PrelevementServiceImpl.getInstance().getChampByTypeAndIdObjet("comboVolFibro", "Prel-" + prelevement.getIdPrelevement());
				if (ccomboVolFibro != null && ccomboVolFibro.getValeur() != null && !ccomboVolFibro.getValeur().equals("null")) {
					tubes += "Quantité :" + ccomboVolFibro.getValeur() + "\r\n\t";
				} else {
					tubes += "Quantité : gros morceau - petit morceau\r\n\t";
				}
				Champ ccomboAspectFibro = PrelevementServiceImpl.getInstance().getChampByTypeAndIdObjet("comboAspectFibro", "Prel-" + prelevement.getIdPrelevement());
				if (ccomboAspectFibro != null && ccomboAspectFibro.getValeur() != null && !ccomboAspectFibro.getValeur().equals("null")) {
					tubes += "Aspect : " + ccomboAspectFibro.getValeur() + "\r\n\t";
				} else {
					tubes += "Aspect : mou - dur - ferme - visqueux\r\n\t";
				}
				Champ champMiseEnCultureF = PrelevementServiceImpl.getInstance().getChampByTypeAndIdObjet("dateCultF", "Prel-" + prelevement.getIdPrelevement());
				if (champMiseEnCultureF != null && champMiseEnCultureF.getValeur() != null && !champMiseEnCultureF.getValeur().equals("null")) {
					SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
					try {
						dmc = formater.parse(champMiseEnCultureF.getValeur());
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
			if (type.equals("S")) {
				Champ nbTubeSang = PrelevementServiceImpl.getInstance().getChampByTypeAndIdObjet("nbSang", "Prel-" + prelevement.getIdPrelevement());

				Integer nbS = Integer.parseInt(nbTubeSang.getValeur());
				for (int i = 0; i < nbS; i++) {

					Champ ccomboTubeSang = PrelevementServiceImpl.getInstance().getChampByTypeAndIdObjet("comboTube-" + i, "Prel-" + prelevement.getIdPrelevement());
					if (ccomboTubeSang != null && ccomboTubeSang.getValeur() != null && !ccomboTubeSang.getValeur().equals("null")) {
						tubes += "Tube " + (i + 1) + " : " + ccomboTubeSang.getValeur() + "\r\n\t";
					} else {
						tubes += "Tube " + (i + 1) + " : hépariné - EDTA - sérum - Paxgene - Tube sec - autre\r\n";
					}
					Champ ccomboVolSang = PrelevementServiceImpl.getInstance().getChampByTypeAndIdObjet("comboVolSang-" + i, "Prel-" + prelevement.getIdPrelevement());
					if (ccomboVolSang != null && ccomboVolSang.getValeur() != null && !ccomboVolSang.getValeur().equals("null")) {
						tubes += "Volume : " + ccomboVolSang.getValeur() + "\r\n\t";
					} else {
						tubes += "Volume : plein - 3/4 - moitié -1/4\r\n\t";
					}

					Champ ccomboAspectSang = PrelevementServiceImpl.getInstance().getChampByTypeAndIdObjet("comboAspectSang-" + i, "Prel-" + prelevement.getIdPrelevement());
					if (ccomboAspectSang != null && ccomboAspectSang.getValeur() != null && !ccomboAspectSang.getValeur().equals("null")) {
						tubes += "Aspect : " + ccomboAspectSang.getValeur() + "\r\n\t";
					} else {
						tubes += "Aspect : absence de caillot - présence de caillot - tube cassé\r\n\t";
					}
					Champ champMiseEnCultureS = PrelevementServiceImpl.getInstance().getChampByTypeAndIdObjet("dateCultS-" + i, "Prel-" + prelevement.getIdPrelevement());
					if (champMiseEnCultureS != null && champMiseEnCultureS.getValeur() != null && !champMiseEnCultureS.getValeur().equals("null")) {
						SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
						try {
							dmc = formater.parse(champMiseEnCultureS.getValeur());
						} catch (ParseException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
			}
			if (type.equals("VC")) {
				Champ ccomboVolVC = PrelevementServiceImpl.getInstance().getChampByTypeAndIdObjet("comboVolVC", "Prel-" + prelevement.getIdPrelevement());
				if (ccomboVolVC != null && ccomboVolVC.getValeur() != null && !ccomboVolVC.getValeur().equals("null")) {
					tubes += "Volume : " + ccomboVolVC.getValeur() + "\r\n";
				} else {
					tubes += "Volume : >20 mg - 10 à 20 mg - <10 mg\r\n";
				}

				Champ ccomboAspectMacroVC = PrelevementServiceImpl.getInstance().getChampByTypeAndIdObjet("comboAspectMacroVC", "Prel-" + prelevement.getIdPrelevement());
				if (ccomboAspectMacroVC != null && ccomboAspectMacroVC.getValeur() != null && !ccomboAspectMacroVC.getValeur().equals("null")) {
					tubes += "Aspect macroscopique : " + ccomboAspectMacroVC.getValeur() + "";
				} else {
					tubes += "Aspect macroscopique :  correct - sanglant - nacré - graisseux - autre";
				}
				Champ ctASpecifierAspectMacroVC = PrelevementServiceImpl.getInstance().getChampByTypeAndIdObjet("tASpecifierAspectMacroVC", "Prel-" + prelevement.getIdPrelevement());
				if (ctASpecifierAspectMacroVC != null && ctASpecifierAspectMacroVC.getValeur() != null && !ctASpecifierAspectMacroVC.getValeur().equals("null")) {
					tubes += "(" + ctASpecifierAspectMacroVC.getValeur() + ")\r\n";
				} else {
					tubes += "\r\n";
				}

				Champ ccomboAspectMicroVC = PrelevementServiceImpl.getInstance().getChampByTypeAndIdObjet("comboAspectMicroVC", "Prel-" + prelevement.getIdPrelevement());
				if (ccomboAspectMicroVC != null && ccomboAspectMicroVC.getValeur() != null && !ccomboAspectMicroVC.getValeur().equals("null")) {
					tubes += "Aspect microscopique : " + ccomboAspectMicroVC.getValeur();
				} else {
					tubes += "Aspect microscopique : correct - caduque - sanglant - friable - autre";
				}
				Champ ctASpecifierAspectMicroVC = PrelevementServiceImpl.getInstance().getChampByTypeAndIdObjet("tASpecifierAspectMicroVC", "Prel-" + prelevement.getIdPrelevement());
				if (ctASpecifierAspectMicroVC != null && ctASpecifierAspectMicroVC.getValeur() != null && !ctASpecifierAspectMicroVC.getValeur().equals("null")) {
					tubes += "(" + ctASpecifierAspectMicroVC.getValeur() + ")\r\n";
				} else {
					tubes += "\r\n";
				}
				tubes += "\r\n";
				Champ cbtnCheckGarderCultureVC = PrelevementServiceImpl.getInstance().getChampByTypeAndIdObjet("btnCheckGarderCultureVC", "Prel-" + prelevement.getIdPrelevement());
				if (cbtnCheckGarderCultureVC != null && cbtnCheckGarderCultureVC.getValeur() != null && !cbtnCheckGarderCultureVC.getValeur().equals("null")) {
					if (Boolean.parseBoolean(cbtnCheckGarderCultureVC.getValeur())) {
						tubes += "Garder les cultures ? Oui\r\n";
					} else {
						tubes += "Garder les cultures ? Non\r\n";
					}
				} else {
					tubes += "Garder les cultures ? Oui - Non\r\n";
				}

				Champ champMiseEnCultureVC = PrelevementServiceImpl.getInstance().getChampByTypeAndIdObjet("dateCultVC", "Prel-" + prelevement.getIdPrelevement());
				if (champMiseEnCultureVC != null && champMiseEnCultureVC.getValeur() != null && !champMiseEnCultureVC.getValeur().equals("null")) {
					SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
					try {
						dmc = formater.parse(champMiseEnCultureVC.getValeur());
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}

			if (type.equals("LA")) {
				Champ nbTubeLA = PrelevementServiceImpl.getInstance().getChampByTypeAndIdObjet("nbLA", "Prel-" + prelevement.getIdPrelevement());

				Integer nbLA = Integer.parseInt(nbTubeLA.getValeur());
				for (int i = 0; i < nbLA; i++) {
					tubes += "Tube " + (i + 1) + " :\r\n\t";
					Champ ccomboVolLA = PrelevementServiceImpl.getInstance().getChampByTypeAndIdObjet("comboVolLA-" + i, "Prel-" + prelevement.getIdPrelevement());
					if (ccomboVolLA != null && ccomboVolLA.getValeur() != null && !ccomboVolLA.getValeur().equals("null")) {
						tubes += "Volume : " + ccomboVolLA.getValeur();
					} else {
						tubes += "Volume : <10 mL - 10 à 20 mL - >20 mL";
					}
					Champ ctASpecifierVolLA = PrelevementServiceImpl.getInstance().getChampByTypeAndIdObjet("tASpecifierVolLA-" + i, "Prel-" + prelevement.getIdPrelevement());
					if (ctASpecifierVolLA != null && ctASpecifierVolLA.getValeur() != null && !ctASpecifierVolLA.getValeur().equals("null")) {
						tubes += "(" + ctASpecifierVolLA.getValeur() + ")\r\n\t";
					} else {
						tubes += "\r\n\t";
					}
					Champ ccomboAspectLA = PrelevementServiceImpl.getInstance().getChampByTypeAndIdObjet("comboAspectLA-" + i, "Prel-" + prelevement.getIdPrelevement());
					if (ccomboAspectLA != null && ccomboAspectLA.getValeur() != null && !ccomboAspectLA.getValeur().equals("null")) {
						tubes += "Aspect du prélèvement : " + ccomboAspectLA.getValeur();
					} else {
						tubes += "Aspect du prélèvement : clair - rosé - hémoragique - verdâtre - blanchâtre - autre";
					}
					Champ ctASpecifierAspectLA = PrelevementServiceImpl.getInstance().getChampByTypeAndIdObjet("tASpecifierAspectLA-" + i, "Prel-" + prelevement.getIdPrelevement());
					if (ctASpecifierAspectLA != null && ctASpecifierAspectLA.getValeur() != null && !ctASpecifierAspectLA.getValeur().equals("null")) {
						tubes += "(" + ctASpecifierAspectLA.getValeur() + ")\r\n\t";
					} else {
						tubes += "\r\n\t";
					}
					Champ ccomboVolCulotLA = PrelevementServiceImpl.getInstance().getChampByTypeAndIdObjet("comboVolCulotLA-" + i, "Prel-" + prelevement.getIdPrelevement());
					if (ccomboVolCulotLA != null && ccomboVolCulotLA.getValeur() != null && !ccomboVolCulotLA.getValeur().equals("null")) {
						tubes += "Volume du culot : " + ccomboVolCulotLA.getValeur() + "\r\n\t";
					} else {
						tubes += "Volume du culot : + / ++ / +++\r\n\t";
					}

					Champ ccomboAspectCulotLA = PrelevementServiceImpl.getInstance().getChampByTypeAndIdObjet("comboAspectCulotLA-" + i, "Prel-" + prelevement.getIdPrelevement());
					if (ccomboAspectCulotLA != null && ccomboAspectCulotLA.getValeur() != null && !ccomboAspectCulotLA.getValeur().equals("null")) {
						tubes += "Aspect du culot : " + ccomboAspectCulotLA.getValeur();
					} else {
						tubes += "Aspect du culot : clair - rosé - autre";
					}
					Champ ctASpecifierAspectCulotLA = PrelevementServiceImpl.getInstance().getChampByTypeAndIdObjet("tASpecifierAspectCulotLA-" + i, "Prel-" + prelevement.getIdPrelevement());
					if (ctASpecifierAspectCulotLA != null && ctASpecifierAspectCulotLA.getValeur() != null && !ctASpecifierAspectCulotLA.getValeur().equals("null")) {
						tubes += "(" + ctASpecifierAspectCulotLA.getValeur() + ")\r\n\t";
					} else {
						tubes += "\r\n\t";
					}

				
				}
				Champ cbtnCheckGarderCultureLA = PrelevementServiceImpl.getInstance().getChampByTypeAndIdObjet("btnCheckGarderCultureLA" , "Prel-" + prelevement.getIdPrelevement());
				if (cbtnCheckGarderCultureLA != null && cbtnCheckGarderCultureLA.getValeur() != null && !cbtnCheckGarderCultureLA.getValeur().equals("null")) {
					if (Boolean.parseBoolean(cbtnCheckGarderCultureLA.getValeur())) {
						tubes += "Garder les cultures ? Oui\r\n\t";
					} else {
						tubes += "Garder les cultures ? Non\r\n\t";
					}
				} else {
					tubes += "Garder les cultures ? Oui - Non\r\n\t";
				}
				Champ champMiseEnCultureLA = PrelevementServiceImpl.getInstance().getChampByTypeAndIdObjet("dateCultLA" , "Prel-" + prelevement.getIdPrelevement());
				if (champMiseEnCultureLA != null) {
					SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
					try {
						dmc = formater.parse(champMiseEnCultureLA.getValeur());
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}

			}
			context.put("tubes", tubes);
			if (dmc != null) {
				context.put("dateMiseCulture", formatter.format(dmc));
			} else {
				context.put("dateMiseCulture", "");
			}

			Date d = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("dd_MM_yyyy");
			File fileout = new File(ApplicationContexte.REPERTOIRE_PROTOCOLE + prelevement.getNumeroPrelevement() + "_" + personne.getNom() + "_" + personne.getPrenom() + "_ficheCulture" + type + "_" + sdf.format(d) + ".docx");
			if (fileout.exists()) {
				creer = MessageDialog.openConfirm(Display.getCurrent().getActiveShell(), "Fichier existant", "Le fichier " + fileout.getName() + " existe déjà souhaitez vous l'écraser?");
			}
			if (creer) {

				OutputStream out = new FileOutputStream(fileout);
				report.process(context, out);
				if (fileout.exists()) {
					nomFichierout += fileout.getName();
				}
			}
			in.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (XDocReportException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return nomFichierout;
	}
}
